package com.cssw.fyzb.busi.api.live;

import com.cssw.fyzb.core.model.vo.live.GiftStatisticsVo;
import com.cssw.fyzb.busi.service.live.GiftService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 主播收到礼物统计(根据礼物id)
 * @author wondream
 */
@Function(description = "主播收到礼物统计(根据礼物id)", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.String, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.String, description = "页大小", required = true)
})
@Component
public class ReceivedGiftStatistics implements Action {

    @Autowired
    GiftService giftService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        Page<Record> page = giftService.receivedGiftStatistics(userId, pageNumber, pageSize);

        List<GiftStatisticsVo> giftStatisticsVos = new ArrayList<>();
        GiftStatisticsVo giftStatisticsVo = null;
        for (Record record: page.getList()){
            giftStatisticsVo = new GiftStatisticsVo();
            giftStatisticsVo.setGiftId(record.getLong("giftId"));
            giftStatisticsVo.setNum(record.getInt("num"));
            giftStatisticsVos.add(giftStatisticsVo);
        }
        return Response.success().put("list", giftStatisticsVos).put("total", page.getTotalPage()).put("count", page.getTotalRow());
    }
}
